package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.TransferViewEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.RequestRSAUtils;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.toast.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class AffirmReceiveTransferActivity extends BaseSwipeBackActivity {

    @BindView(R.id.state_title)
    TextView stateTitle;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.affirm_receive)
    TextView receive;
    @BindView(R.id.state_hint_receive_no)
    TextView stateHintReceiveNo;
    @BindView(R.id.state_hint_received)
    TextView stateHintReceived;
    @BindView(R.id.transfer_time)
    TextView transferTime;
    @BindView(R.id.receive_time)
    TextView receiveTime;

    @BindView(R.id.state_img)
    ImageView stateImg;

    /**
     * \
     * <p>
     * //     * 0       转款者界面，未接收
     * //     * 1       转款者界面，已接受
     * //     * 2       收款者界面，未接收
     * //     * 3       收款者界面，已接受
     */
    private int stateType = 0;
    /**
     * 0      发送方
     * 1      收到方
     */
    private int type = 0;
    /**
     * 转账id
     */
    private String transferId;
    PGService mPgService;
    private TransferViewEntivity mTransferViewEntivity;

    /**
     * 开启这个Activity 需要传一个状态值  stateType
     */
    @Override
    protected String getTitleText() {
        return getString(R.string.transfer_details);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        mPgService = PGService.getInstance();
        type = getIntent().getIntExtra("type", 0);
        transferId = getIntent().getStringExtra("transferId");
        viewTransfer(transferId);

    }


    @Override
    public int getContentViewId() {
        return R.layout.activity_affirm_receive_transfer;
    }

    @OnClick({R.id.affirm_receive})
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.affirm_receive:
                // 做 收钱 操作
                confimTransfer(transferId);
                break;
        }
    }

    /**
     * 获取转账状态
     *
     * @param uId
     */
    public void viewTransfer(final String uId) {
//        Log.i("-----", "viewTransfer: uId"+uId);
        showProgress(getString(R.string.please_wait));
        mPgService.viewTransfer(ToolsUtils.getMyUserId(), uId).subscribe(new AbsAPICallback<TransferViewEntivity>() {
            @Override
            protected void onResultError(ApiException ex) {
                hideProgress();
                new ToastUtils().showSingleToast(getString(R.string.get_data_defeat));
            }

            @Override
            public void onNext(TransferViewEntivity data) {
//                new ToastUtils().showSingleToast(data.getInfo());
                mTransferViewEntivity = data;
                if (0 == type) {
                    if ("1".equals(data.getStatus())) {
                        stateType = 0;
                    } else if ("2".equals(data.getStatus())) {
                        stateType = 1;
                    } else if ("3".equals(data.getStatus())) {
                        stateType = 4 ;//转款者超时
                        showToast(getString(R.string.transfer_already_overtime));
                    }
                } else if (1 == type) {
                    if ("1".equals(data.getStatus())) {
                        stateType = 2;
                    } else if ("2".equals(data.getStatus())) {
                        stateType = 3;
                    } else if ("3".equals(data.getStatus())) {
                        stateType = 5 ;//接受者超时
                        showToast(getString(R.string.transfer_already_overtime));
                    }
                }
                hideProgress();
                updateUI();
            }
        });
    }

    /**
     * 确认收钱
     *
     * @param uId
     */
    public void confimTransfer(final String uId) {
        String userId=ToolsUtils.getMyUserId();
        com.alibaba.fastjson.JSONObject json = new JSONObject();
        json.put("userId", userId);
        json.put("transId", uId);
        String content = RequestRSAUtils.setRsaContent(json);
        showProgress(getString(R.string.please_wait));
        mPgService.confimTransfer(content).subscribe(new AbsAPICallback<TransferViewEntivity>() {
        //mPgService.confimTransfer(ToolsUtils.getMyUserId(), uId).subscribe(new AbsAPICallback<TransferViewEntivity>() {
            @Override
            protected void onResultError(ApiException ex) {
                hideProgress();
                new ToastUtils().showSingleToast(getString(R.string.get_data_defeat));
            }

            @Override
            public void onNext(TransferViewEntivity data) {
                new ToastUtils().showSingleToast(getString(R.string.receive_success));
                Intent intent = new Intent();
                intent.putExtra("TransferViewEntivity", mTransferViewEntivity);
                setResult(10, intent);
                scrollToFinishActivity();
//                viewTransfer(transferId);
            }
        });
    }

    private void updateUI() {
        switch (stateType) {
            case 0://0
                stateImg.setImageResource(R.drawable.wait);
                stateImg.setVisibility(View.VISIBLE);
                if (null != mTransferViewEntivity) {

                    ImFriendEntivity mImFriendEntivity = ToolsUtils.getMyFriendForId(Long.parseLong(mTransferViewEntivity.getDestId()));
                    if (null != mImFriendEntivity) {
                        stateTitle.setText(getString(R.string.stay) + ToolsUtils.getNick(mImFriendEntivity) + getString(R.string.affirm_receive));
                    }
                    money.setText("￥ " + mTransferViewEntivity.getAmount());
                    transferTime.setText(getString(R.string.transfer_time) + ToolsUtils.getStrTimeFlong(mTransferViewEntivity.getSendTime()));
                }
                stateHintReceiveNo.setText(R.string.hint_24hour_opposite_no_back_to_you);
                stateHintReceiveNo.setVisibility(View.VISIBLE);
                receiveTime.setVisibility(View.GONE);
                transferTime.setVisibility(View.VISIBLE);

                break;
            case 1://0
                stateImg.setImageResource(R.drawable.sure);
                stateImg.setVisibility(View.VISIBLE);
                if (null != mTransferViewEntivity) {
                    ImFriendEntivity mImFriendEntivity = ToolsUtils.getMyFriendForId(Long.parseLong(mTransferViewEntivity.getDestId()));
                    if (null != mImFriendEntivity) {
                        stateTitle.setText(ToolsUtils.getNick(mImFriendEntivity) + getString(R.string.aleardy_receive_money));
                    }
                    money.setText("￥ " + mTransferViewEntivity.getAmount());
                    transferTime.setText(getString(R.string.transfer_time) + ToolsUtils.getStrTimeFlong(mTransferViewEntivity.getSendTime()));
                    transferTime.setVisibility(View.VISIBLE);
                    if (null == mTransferViewEntivity.getConfirmTime() || "".equals(mTransferViewEntivity.getConfirmTime())) {
                        receiveTime.setVisibility(View.GONE);
                    } else {
                        receiveTime.setText(getString(R.string.collection_time) + ToolsUtils.getStrTimeFlong(mTransferViewEntivity.getConfirmTime()));
                        receiveTime.setVisibility(View.VISIBLE);
                    }
                }
                stateHintReceiveNo.setVisibility(View.GONE);
                stateHintReceived.setVisibility(View.VISIBLE);
                stateHintReceived.setText(R.string.money_aleardy_to_other_balance);

                break;
            case 2://0
                stateImg.setImageResource(R.drawable.wait);
                stateImg.setVisibility(View.VISIBLE);
                stateTitle.setText(R.string.stay_receive_money);
                if (null != mTransferViewEntivity) {
                    money.setText("￥ " + mTransferViewEntivity.getAmount());
                    transferTime.setText(getString(R.string.transfer_time) + ToolsUtils.getStrTimeFlong(mTransferViewEntivity.getSendTime()));
                    transferTime.setVisibility(View.VISIBLE);
                }
                receive.setVisibility(View.VISIBLE);
                stateHintReceiveNo.setText(R.string.hint_24hour_no_affirm_money_back_to_other);
                stateHintReceiveNo.setVisibility(View.VISIBLE);
                stateHintReceived.setVisibility(View.VISIBLE);
                stateHintReceived.setText(R.string.money_aleardy_deposit_other_balance);
                receiveTime.setVisibility(View.GONE);
                break;
            case 3:
                stateImg.setImageResource(R.drawable.sure);
                stateImg.setVisibility(View.VISIBLE);
                if (null != mTransferViewEntivity) {
                    stateTitle.setText(getString(R.string.aleardy_receive_money));
                    money.setText("￥ " + mTransferViewEntivity.getAmount());
                    transferTime.setText(getString(R.string.transfer_time) + ToolsUtils.getStrTimeFlong(mTransferViewEntivity.getSendTime()));
                    transferTime.setVisibility(View.VISIBLE);
                    if (null == mTransferViewEntivity.getConfirmTime() || "".equals(mTransferViewEntivity.getConfirmTime())) {
                        receiveTime.setVisibility(View.GONE);
                    } else {
                        receiveTime.setText(getString(R.string.collection_time) + ToolsUtils.getStrTimeFlong(mTransferViewEntivity.getConfirmTime()));
                        receiveTime.setVisibility(View.VISIBLE);
                    }
                }
                stateHintReceiveNo.setVisibility(View.GONE);
                stateHintReceived.setVisibility(View.VISIBLE);
                stateHintReceived.setText(getString(R.string.money_aleardy_deposit_other_balance));
                break;
            case 4://0
                stateImg.setImageResource(R.drawable.wait);
                stateImg.setVisibility(View.VISIBLE);
                stateTitle.setText(getResources().getString(R.string.affirm_out_time));
                stateHintReceiveNo.setText(R.string.hint_24hour_opposite_no_back_to_you);
                stateHintReceiveNo.setVisibility(View.VISIBLE);
                receiveTime.setVisibility(View.GONE);
                transferTime.setText(getString(R.string.transfer_time) + ToolsUtils.getStrTimeFlong(mTransferViewEntivity.getSendTime()));
                transferTime.setVisibility(View.VISIBLE);

                break;
            case 5://0
                stateImg.setImageResource(R.drawable.wait);
                stateImg.setVisibility(View.VISIBLE);
                stateTitle.setText(getResources().getString(R.string.affirm_out_time));
                stateHintReceiveNo.setText(R.string.hint_24hour_no_affirm_money_back_to_other);
                stateHintReceiveNo.setVisibility(View.VISIBLE);
                receiveTime.setVisibility(View.GONE);
                transferTime.setText(getString(R.string.transfer_time) + ToolsUtils.getStrTimeFlong(mTransferViewEntivity.getSendTime()));
                transferTime.setVisibility(View.VISIBLE);

                break;
        }
    }
}
